/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package binh;
import we.session.CSession;

/**
 *
 * @author ADMIN
 */
public class CClientMgr
{

    private CClient[] ClientList;
    private int CCU;

    public CClientMgr(int MaxClient)
    {
        ClientList = new CClient[MaxClient];
        for (int i = 0; i < ClientList.length; i++)
        {
            ClientList[i] = new CClient(i);
        }
        CCU = 0;        
    }

    synchronized public boolean AddSlot(CSession session)
    {
        if (CCU == ClientList.length)
        {
            return false;
        }
        for (int i = 0; i < ClientList.length; i++)
        {
            if (!ClientList[i].IsUsed())
            {
                ClientList[i].Join(session);
                CCU++;
                session.setAttribute(CBinhHandler.ATT_CLIENT_INDEX, i);
                return true;
            }
        }

        return false;
    }

    synchronized public boolean removeSlot(int Index)
    {
        if (ClientList[Index].IsUsed())
        {            
            ClientList[Index].dispose();
            CCU--;
            return true;
        } else
        {
            return false;
        }
    }      
    
    public CClient getClient(int Index){
        return ClientList[Index];
    }
    
    public int GetCCU(){
        return CCU;
    }
}
